
const util = require('../../../../../../utils/util.js');
const api = require('../../../../../../config/api.js');

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    seedid: '',// 选中的种子id
    seedname: '',// 选中的种子name
    lockSeedInfo: { "seed": {}, "lockSeedDialog": false, "isLockOk": true },//需要解锁的种子数据
    seedIndex: {},// 需要解锁的种子下标
    seedList: [],//所有的种子
    userSeedList: [],//用户的种子数据
    descDialogShow: false,// 描述弹窗
    userCapital: {},//用户的资金信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    _this.setData({
      seedid: options.seedid,
      seedname: options.seedname,
      joinCount: options.joinCount
    })
    // 获取种子数据
    _this.getAllSeed();
    // 获取用户资金信息
    _this.getUserCapital();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 获取用户所有种子
   */
  getAllSeed: function () {
    var _this = this;
    util.post(api.Interest_GetAllSeedUrl).then(function (res) {
      if (res.code == 200) {
        var seedList = res.result.list;
        for (var i = 0; i < seedList.length; i++) {
          if (seedList[i].needOrangeshell != null){
            seedList[i].needOrangeshell = parseInt(seedList[i].needOrangeshell);
          }
        }
        _this.setData({
          seedList: seedList
        })
        _this.getSeedinfoByOpenid();
      }
    })
  },

  /**
   * 获取用户的种子信息
   */
  getSeedinfoByOpenid: function () {
    var _this = this;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var param = {};
        param.access_token = res.access_token;
        param.openid = openid;
        util.post(api.Interest_GetSeedinfoByOpenidApiUrl, param).then(function (res) {
          if (res.code == 200) {
            var seedList = _this.data.seedList;
            var userSeedList = res.result.list;
            _this.setData({
              userSeedList: res.result.list
            })
            for (var i = 0; i < seedList.length; i++) {
              for (var j = 0; j < userSeedList.length; j++) {
                if (seedList[i].id == userSeedList[j].seedid) {
                  _this.setData({
                    ["seedList[" + i + "].isUnlock"]: 1
                  })
                }
              }
            }
          }
        })
      })
    }
  },

  /**
   * 获取用户资金信息
   */
  getUserCapital: function () {
    var _this = this;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var param = {};
        param.access_token = res.access_token;
        param.openid = openid;
        util.post(api.My_GetUserCapitalApiUrl, param).then(function (res) {
          if (res.code == 200) {
            var userCapital = res.result.entity;
            if (userCapital.enableIntegral != null){
              userCapital.enableIntegral = parseInt(userCapital.enableIntegral);
            }else{
              userCapital.enableIntegral = 0;
            }
            _this.setData({
              userCapital: userCapital
            })
          }
        })
      })
    }
  },

  /**
   * 种子选中事件
   */
  seedTap: function(e){
    var _this = this;
    var index = e.currentTarget.dataset.index;
    var seed = _this.data.seedList[index];
    if (seed.isUnlock == 0){
      // 需要解锁
      var isLockOk = parseInt(seed.needOrangeshell) > parseInt(_this.data.userCapital.enableIntegral) ? false : true;
      var lockSeedInfo = { "seed": seed, "lockSeedDialog": true, "isLockOk": isLockOk }
      _this.setData({
        lockSeedInfo: lockSeedInfo
      })
    } else {
      // 不需要解锁
      _this.setData({
        seedid: seed.id,
        seedname: seed.name
      })
    }
  },

  /**
   * 描述弹窗的展开与关闭
   */
  descDialogShow: function(){
    var _this = this;
    _this.setData({
      descDialogShow: !_this.data.descDialogShow
    })
  },

  /**
   * 解锁种子的弹窗关闭
   */
  lockSeedDialogClose: function () {
    var _this = this;
    var lockSeedInfo = { "seed": {}, "lockSeedDialog": false, "isLockOk": true };
    _this.setData({
      lockSeedInfo: lockSeedInfo
    })
  },

  /**
   * 解锁种子
   */
  unLockSeed: function(){
    var _this = this;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var param = {};
        param.access_token = res.access_token;
        param.openid = openid;
        param.seedid = _this.data.lockSeedInfo.seed.id;
        util.post(api.Interest_UnLockSeedApiUrl, param).then(function (res) {
          if (res.code == 200) {
            util.showToast("开启成功");
            // 关闭弹窗
            _this.lockSeedDialogClose();
            // 获取种子数据
            _this.getAllSeed();
            // 获取用户资金信息
            _this.getUserCapital();
          }
        })
      })
    }
  },

  /**
   * 提交并返回上一页
   */
  submit: function(){
    var _this = this;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      ["circleInfo.seedid"]: _this.data.seedid,
      ["circleInfo.seedname"]: _this.data.seedname
    });
    wx.navigateBack({
      delta: 1
    })
  }
})